// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import MintUI from 'mint-ui'
import './js/iconfont.js'
import 'mint-ui/lib/style.css'
import './css/reset.css'
import './js/component'
import App from './App'
import router from './js/router'
import config from './js/config'
import util from './js/util'
import ajax from './js/ajax'
import wechat from './js/wechat'
import logger from './js/logger'
import echarts from "echarts"
import iView from 'iview'  //导入组件库

import 'iview/dist/styles/iview.css'  // 导入样式

Vue.use(iView)
// EChart


Vue.use(MintUI);
Vue.use(echarts);

window.app = Object.assign({}, {config, util, ajax, wechat, logger});

Vue.config.productionTip = false;

/* eslint-disable no-new */
const initVue = function () {
    window.vm = new Vue({
        el: '#app',
        router,
        // store,
        components: {App},
        template: '<App/>'
    });
};

const initUnPlusParam = function () {
    if (/micromessenger/i.test(navigator.userAgent)) {    // 微信内置浏览器
        config.setting.platform = "wechat";

        // 去掉顶部状态栏高度
        config.setting.height.display = window.innerHeight + config.setting.height.top;

        // 初始化微信支付配置
        wechat.initConfig();

        //监听微信返回键
        window.addEventListener("popstate", (e) => {
            if (vm.$route.params.id) {
                if (document.title === '用水详情') {
                    if (vm.$route.params.id === '用量') {
                        vm.$router.push({name: 'index', params: {id: '用量'}})
                    } else {
                        vm.$router.push({
                            name: 'waterCharge', params: {id: 'index'}
                        })
                    }
                } else if (document.title === '户号绑定') {
                    vm.$router.push({
                        name: 'accountBinding', params: {id: 'index'}
                    })
                } else if (document.title === '缴费') {
                    if (vm.$route.params.id === 'index') {
                        vm.$router.push({name: vm.$route.params.id});
                    } else {
                        vm.$router.push({
                            name: 'waterRemainder', params: {id: vm.$route.params.id}
                        })
                    }
                } else if (vm.$route.params.id === '首页') {
                    vm.$router.push({name: 'index', params: {id: '首页'}})
                } else if (vm.$route.params.id === '用量') {
                    vm.$router.push({name: 'index', params: {id: '用量'}})
                } else {
                    vm.$router.push({name: vm.$route.params.id});
                }
            } else {
                if (document.title === '缴费' || document.title === '账户余额' || document.title === '缴费记录' || document.title === '用水统计' || document.title === '切换户号') {
                    vm.$router.push({name: 'index', params: {id: '用量'}})
                } else if (document.title === '绑定户号' || document.title === '业务记录' || document.title === '我的消息' || document.title === '公告通知' || document.title === '常见问题') {
                    vm.$router.push({name: 'index', params: {id: '服务'}})
                } else if (document.title === '密码修改' || document.title === '参数设置' || document.title === '意见反馈' || document.title === '推荐分享' || document.title === '关于我们' || document.title === '个人信息') {
                    vm.$router.push({name: 'index', params: {id: '我的'}})
                } else {
                    if (document.title === '户号绑定' && !util.localStorage('user.householdNo')) {
                        vm.$router.push({name: 'accountBinding02'})
                    }
                }
            }
        }, false);
    } else {
        config.setting.platform = "other";
    }

    initVue();
};

const initMobileParam = function () {
    if (navigator.userAgent.indexOf('Html5Plus') > -1) {
        if (window.plus) {
            initPlusParam();
        } else {
            document.addEventListener('plusready', initPlusParam, false);
        }
    } else {
        initUnPlusParam();
    }
}

const initPlusParam = function () {
    config.setting.platform = 'mobile';
    //手机状态栏高度
    config.setting.statusHeight = window.plus.navigator.getStatusbarHeight();
    //注册返回按键事件
    window.plus.key.addEventListener('backbutton', function () {
        //退出
        if (vm.$router.currentRoute.name === 'index' || vm.$router.currentRoute.name === 'login') {
            vm.$messagebox.confirm('确定退出当前应用?').then(action => {
                window.plus.runtime.quit();
            }).catch(err => {
                console.log('exit', err);
            });
        } else {
            vm.$router.back();
        }
    }, false);

    // 获取APP版本
    window.plus.runtime.getProperty(window.plus.runtime.appid, (inf) => {
        config.setting.version = inf.version;

        app.ajax.get(app.config.setting.serverMobile + "version", {
            version: config.setting.version
        }, (res) => {
            if (res.s === 0) {
                config.setting.newVersion = true;
                config.setting.wgtUrl = res.data;

                window.plus.nativeUI.confirm("发现新版本，是否更新?", function (e) {
                    if (e.index === 0) {
                        // 下载wgt文件
                        app.util.upgradeWgt(res.data);
                    }
                }, "新版本更新提示", ["更新", "取消"]);
            }
        });
    });

    initVue();
}

if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {  // 移动端
    initMobileParam();
} else {
    initVue();
}

Vue.prototype.$echarts=echarts;
Vue.prototype.$echartsResize=function (ref) {
    window.addEventListener("resize",function () {
        ref.resize();
    })
};
